package com.org.gsc.chartdemo.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.org.gsc.chartdemo.listeners.OnclickLister;

/**
 * Created by qundui on 2017/8/29.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.VH> {
    private String Tags[];
    private Context ctx;

    public MyAdapter(String[] tags,Context ctx) {
        Tags = tags;
        this.ctx=ctx;
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        return new VH(LayoutInflater.from(ctx).inflate(android.R.layout.simple_list_item_1,parent,false));
    }

    public void setOnclickLister(OnclickLister onclickLister) {
        mOnclickLister = onclickLister;
    }

    private OnclickLister mOnclickLister;
    @Override
    public void onBindViewHolder(final VH holder, int position) {
        holder.tv.setText(Tags[position]);
        if (mOnclickLister!=null){
            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnclickLister.OnItemclickLister(holder.getLayoutPosition(),holder.tv);
                }
            });
        }
        if (mOnclickLister!=null){
           holder.tv.setOnLongClickListener(new View.OnLongClickListener() {
               @Override
               public boolean onLongClick(View v) {
                   mOnclickLister.OnItemLongclickListers(holder.getLayoutPosition(),holder.tv);
                   return false;
               }
           });
        }
    }
    @Override
    public int getItemCount() {
        return Tags.length;
    }
    public class VH extends RecyclerView.ViewHolder {
        private TextView tv;
        public VH(View itemView) {
            super(itemView);
            tv= (TextView) itemView.findViewById(android.R.id.text1);
        }
    }
}
